package com.sogoodsoft.web.project;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.sogoodsoft.entity.account.User;
import com.sogoodsoft.entity.app.SysClass;
import com.sogoodsoft.entity.project.Assessment;
import com.sogoodsoft.service.app.SysClassService;
import com.sogoodsoft.service.project.AssessmentService;
import com.sogoodsoft.util.InfosysSecurityUtil;
import com.sogoodsoft.web.CrudActionSupport;
import com.sogoodsoft.web.WebConstants;

@Namespace("/project")
@Results({ @Result(name = CrudActionSupport.RELOAD, location = "assessment.action", type = "redirect"),
        @Result(name = "readPRJ", location = "readprj.jsp") })
public class AssessmentAction extends CrudActionSupport < Assessment > {

    /**
	 * 
	 */
    private static final long serialVersionUID = -7761542961040155066L;

    private Long id;

    private Assessment assessment;

    private AssessmentService assemessSer;

    private Long resultId;

    private SysClass result;

    private SysClassService sysSer;

    private User user;

    private Page < Assessment > page = new Page < Assessment >(WebConstants.PAGE_SIZE);

    @Override
    public String delete() throws Exception {
        // prepare();
        // assemessSer.delete(assessment);

        return RELOAD;
    }

    @Override
    public String input() throws Exception {
        return INPUT;
    }

    public String readPRJ() throws Exception {
        prepareModel();
        return "readPRJ";
    }

    @SuppressWarnings("unchecked")
    @Override
    public String list() throws Exception {
        // List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
        //
        // //设置默认排序方式
        // if (!page.isOrderBySetted()) {
        // page.setOrderBy("id");
        // page.setOrder(Page.DESC);
        // }
        // logger.debug("查询申核");
        // page = assemessSer.findPage(page, filters);

        logger.debug("查询审核列表");
        page = assemessSer.keyQuery("list.assessment", page, Struts2Utils.getRequest().getParameterMap());
        return SUCCESS;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            assessment = assemessSer.get(id);
        } else {
            assessment = new Assessment();
        }
        if (resultId != null) {
            result = sysSer.get(resultId);
            assessment.setSysClass(result);
        }
        user = assessment.getUser();
        if (user == null && InfosysSecurityUtil.getCurrentUser() != null) {
            user = InfosysSecurityUtil.getCurrentUser();
            assessment.setUser(user);
        }
    }

    @Override
    public String save() throws Exception {
        assemessSer.save(assessment);
        return RELOAD;
    }

    /**
     * 非管理员，只能审核本部门的项目
     */
    public void validateSave() {
        User user = InfosysSecurityUtil.getCurrentUser();
        assessment = assemessSer.get(id);
        if (user.getRoleNames().indexOf("管理员") < 0
                && assessment.getOrganization().getId().intValue() != user.getOrganization().getId().intValue()) {
            this.addActionError(getText("assessment.action.save.err"));
        }

    }

    @Override
    public Assessment getModel() {
        return assessment;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Assessment getAssessment() {
        return assessment;
    }

    public void setAssessment(Assessment assessment) {
        this.assessment = assessment;
    }

    public AssessmentService getAssemessSer() {
        return assemessSer;
    }

    @Autowired
    public void setAssemessSer(AssessmentService assemessSer) {
        this.assemessSer = assemessSer;
    }

    public Page < Assessment > getPage() {
        return page;
    }

    public void setPage(Page < Assessment > page) {
        this.page = page;
    }

    public Long getResultId() {
        return resultId;
    }

    public void setResultId(Long resultId) {
        this.resultId = resultId;
    }

    public SysClass getResult() {
        return result;
    }

    public void setResult(SysClass result) {
        this.result = result;
    }

    public SysClassService getSysSer() {
        return sysSer;
    }

    @Autowired
    public void setSysSer(SysClassService sysSer) {
        this.sysSer = sysSer;
    }

}
